classdef Attack
    %   攻击类
    properties
        Image %%将被篡改的隐形图片
    end
    
    methods
        function obj=Attack(Image)
            obj.Image=Image;
        end
        %% 剪切攻击，剩下的用128补充
        function tamper = cropattack(obj,X,Y,range)
            %METHOD1 此处显示有关此方法的摘要
            %   X,Y为剪切的行与列
            %   range代表范围
            [h,w,k]=size(obj.Image);
            if X+range>h || Y+range>w
                disp('inputs size is exceeding the cover length');
                obj.Image=0;
                return;
            end
            %原图剪切
            for i=1:k
                obj.Image(X:X+range,Y:Y+range,i)=128;
            end
            tamper=obj.Image;
        end
        %% 替换攻击,用自身图形的位置替换
        function tamper = replaceattack_orgin(obj,i,j,new_i,new_j,block_size)
        fr=i:1:i+block_size;%行截取
        fc=j:1:j+block_size;%列截取
        imgNew = obj.Image(fr, fc);%获取截取子图
        replaceImageattack = obj.Image;
        fnr = new_i:1:new_i+block_size;%要替换的位置
        fnc = new_j:1:new_j+block_size;
        replaceImageattack(fnr, fnc) = imgNew;
        tamper=replaceImageattack;
        end
        %% 替换攻击，用新的图片来替换特定位置
        function tamper = replaceattack_news(obj,i,j,img)
            [col,raw]=size(img);
            obj.Image(i:i+col-1,j:j+raw-1)=img;
            tamper=obj.Image;
        end
        %% 替换攻击，文字替换
%         function tamper = replaceattack_word(obj,i,j,img)
%             [col,raw]=size(img);
%             obj.Image(i:i+col-1,j:j+raw-1)=img;
%             tamper=obj.Image;
%         end
        %% 随机篡改全区域
        function tamper = rand_tamper(obj,density)
        %   density修改密度，10,20,30,40
        [col,raw]=size(obj.Image);
        tamper=obj.Image;
        tamper_num=round(col*raw*density/100);
        for w=1:tamper_num
            i= randi([1,col],1,1);
            j= randi([1,raw],1,1);
            x= randi([0,255],1,1);
            tamper(i,j)=x;
        end
        end
        %% 特殊图形替换攻击
        function tamper = replace_img(obj,module)
        %UNTITLED2 此处显示有关此函数的摘要
        %   module要求与原始的stego_cover大小一致
        tamper=obj.Image;
        [col,raw]=size(obj.Image);
        for i=1:col
            for j=1:raw
                if module(i,j)>15
                    tamper(i,j)=248;
                end
            end
        end
        end
        %% 修改特定区域
        function tamper = area_tamper(obj,density)
        %   density修改密度，10,20,30,40。代表修改图片的多少列
        %   举例：density=20。raw=256,那么就是修改图片的round(256*20%)=51列，按从左往右
        [col,raw]=size(obj.Image);
        tamper=obj.Image;
        tamper_raw=round(raw*density/100);
        for i=1:col
            for j=1:tamper_raw
                x= randi([0,255],1,1);
                tamper(i,j)=x;
            end
        end
        end
        %% JPEG compression Attack
        function jpegImageAttacked = jpegAttack(obj,qualityFactor)
        imwrite(obj.Image,'jpegImageAttacked.jpg','jpg','quality',qualityFactor);
        jpegImageAttacked = imread('jpegImageAttacked.jpg');
        end
        %% Gaussian Noise Attack
        function GaussNoiseImageAttacked = noiseGauss(obj,var)
        convert=obj.Image./255;
        GaussNoiseImageAttacked = imnoise(convert, 'gaussian', 0,var);
        %f为是输入图像。函数imnoise在给图像添加噪声之前，将它转换为范围[0,1]内的double类图像。
        %指定噪声参数时必须考虑到这一点。 
        %g=imnoise(f，‘gaussian’，m，var)是将均值为m，方差为var的高斯噪声加到图像f上。
        %m的默认值是0，var默认值是0.01。
        GaussNoiseImageAttacked=GaussNoiseImageAttacked.*255;
        end
    end
end

